package nsh.cl.nd.khtn.mazedash.baseclasses;

import org.andengine.engine.camera.Camera;

public class ShakeCamera extends Camera {

	private boolean shaking;
	private float duration;
	private float intensity;
	private float currentDuration;

	private float x;
	private float y;

	public ShakeCamera(float pX, float pY, float pWidth, float pHeight) {
		super(pX, pY, pWidth, pHeight);
		shaking = false;

		x = this.getCenterX();
		y = this.getCenterY();
	}

	public void shake(float duration, float intensity) {
		shaking = true;
		this.duration = duration;
		this.intensity = intensity;
		currentDuration = 0;
	}

	@Override
	public void onUpdate(float pSecondsElapsed) {
		super.onUpdate(pSecondsElapsed);

		if (shaking) {
			currentDuration += pSecondsElapsed;
			if (currentDuration > duration) {
				shaking = false;
				currentDuration = 0;
				this.setCenter(x, y);
			} else {
				int sentitX = 1;
				int sentitY = 1;
				if (Math.random() < 0.5)
					sentitX = -1;
				if (Math.random() < 0.5)
					sentitY = -1;
				this.setCenter((float) (x + Math.random() * intensity * sentitX), (float) (y + Math.random()
						* intensity * sentitY));
			}
		}
	}
}